package com.tianji.learning.controller;


import cn.hutool.db.PageResult;
import com.tianji.api.dto.leanring.LearningLessonDTO;
import com.tianji.common.domain.dto.PageDTO;
import com.tianji.common.domain.query.PageQuery;
import com.tianji.common.utils.CollUtils;
import com.tianji.learning.domain.dto.LearningPlanDTO;
import com.tianji.learning.domain.vo.LearningLessonVO;
import com.tianji.learning.domain.vo.LearningPlanPageVO;
import com.tianji.learning.service.ILearningLessonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 学生课程表 前端控制器
 * </p>
 *
 * @author 丁真
 * @since 2025-06-27
 */
@Api(tags = "我的课表相关接口")
@Slf4j
@RestController
@RequestMapping("lessons")
@RequiredArgsConstructor
public class LearningLessonController {
    private final ILearningLessonService lessonService;

    /**
     * 分页查询我的课表
     *
     * @param pageQuery
     * @return
     */
    @ApiOperation("分页查询我的课表")
    @GetMapping("/page")
    public PageDTO<LearningLessonVO> pageLessons(PageQuery pageQuery) {
        log.info("分页查询我的课表：{}", pageQuery);
        return lessonService.pageLessons(pageQuery);
    }

    /**
     * 查询我最近正在学习的课程
     *
     * @return
     */
    @ApiOperation("查询我最近正在学习的课程")
    @GetMapping("/now")
    public LearningLessonVO now() {
        log.info("查询我正在学习的课程");
        return lessonService.now();
    }

    /**
     * 创建学习计划
     *
     * @param planDTO
     */
    @ApiOperation("创建学习计划")
    @PostMapping("/plans")
    public void savePlans(@RequestBody LearningPlanDTO planDTO) {
        log.info("创建学习计划：{}", planDTO);
        lessonService.savePlans(planDTO);
    }

    /**
     * 统计查询我的学习计划
     *
     * @param pageQuery
     * @return
     */
    @ApiOperation("统计查询我的学习计划")
    @GetMapping("/plans")
    public LearningPlanPageVO pagePlans(PageQuery pageQuery) {
        log.info("分页查询学习计划：{}", pageQuery);
        return lessonService.pagePlans(pageQuery);
    }

    /**
     * 用户删除课程
     *
     * @param courseId
     */
    @ApiOperation("用户删除课程")
    @DeleteMapping("/{courseId}")
    public void deleteCourseFromLesson(@PathVariable Long courseId) {
        log.info("用户删除课程：{}", courseId);
        lessonService.removeUserLessons(null, CollUtils.singletonList(courseId));
    }

    /**
     * 校验当前用户是否可以学习当前课程
     *
     * @param courseId
     * @return
     */
    @ApiOperation("校验当前用户是否可以学习当前课程")
    @GetMapping("/{courseId}/valid")
    public Long isLessonValid(@PathVariable("courseId") Long courseId) {
        log.info("校验当前用户是否可以学习当前课程{}", courseId);
        return lessonService.isLessonValid(courseId);
    }

    /**
     * 查询用户课表中指定课程状态
     *
     * @param courseId
     * @return
     */
    @ApiOperation("查询用户课表中指定课程状态")
    @GetMapping("/{courseId}")
    public LearningLessonVO searchStatus(@PathVariable("courseId") Long courseId) {
        return lessonService.searchStatus(courseId);
    }

    /**
     * 统计课程学习人数
     *
     * @param courseId
     * @return
     */
    @ApiOperation("统计课程学习人数")
    @GetMapping("/{courseId}/count")
    public Integer countLearningLessonByCourse(@PathVariable("courseId") Long courseId){
        return lessonService.countLearningLessonByCourse(courseId);
    }
}
